import {Component, OnInit, ViewEncapsulation, ViewChild, OnDestroy} from '@angular/core';
import {Subscription} from "rxjs";
import {Content} from "@shared/components/toolbar/toolbar-content";
import {Router, ActivatedRoute, Params} from "@angular/router";
import {HomepageService} from "@homepage/homepage.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {ToolbarService} from "@shared/components/toolbar/toolbar.service";
import {ImageServeService} from "@shared/service/image/image-serve.service";
import {HomepageFansComponent} from "@homepage/homepage-fans/homepage-fans.component";
import {Subject} from "rxjs/Subject";


@Component({
  selector: 'app-homepage',
  templateUrl: './homepage.component.html',
  styleUrls: ['./homepage.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class HomepageComponent implements OnInit, OnDestroy {
  public tabs: any;
  public personInfo: any;
  public isFollowedMe: number;
  public isFollowed: number;
  public userId: string;
  public isLogin: string;
  public userIcon: string;
  public tabStore = {
    currentTabTitle: '分享',
    loadedTabArr: [],
    unableTabArr: [],
    noDataTabArr: ['粉丝', '服务', '赛事']
  };
  @ViewChild(HomepageFansComponent) fansTab;
  subscription: Subscription = new Subscription();
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  @ViewChild(Content) content;

  constructor(
    public router: Router,
    public homepageService: HomepageService,
    public _activatedRoute: ActivatedRoute,
    public toolsService: ToolsService,
    public user: AuthService,
    public toolBarService: ToolbarService,
    public imgService: ImageServeService
  ) {
    this.userIcon = this.imgService.getAvatarIcon();
    this.isLogin = this.user.getUserId();
    this.toolsService.showLoading();

    this._activatedRoute.params.subscribe((params: Params) => {
      this.userId = params['userId'];
      this.getInfo(params['userId']);
    });

    this.tabs = [
      {title: '分享', content: 'content1'},
      {title: '粉丝', content: 'content2'},
      {title: '服务', content: 'content3'},
      {title: '赛事', content: 'content3'}
    ];
  }

  /**
   * @description 选择tab
   * @author baoww765
   * @date 2017-12-16
   * @param 被点击tab
   * */
  select(tab) {
    const title = tab.title;
    this.tabStore.currentTabTitle = title;
    if (this.tabStore.loadedTabArr.indexOf(title) > -1) {
      return;
    }
    this.loadData(title, this.userId);
    this.tabStore.loadedTabArr.push(tab.title);
  }

  /**
   * @description 处理点击tab，调取对应的函数获取数据
   * @author baoww765
   * @date 2017-12-16
   * @param title, userId
   * */
  loadData(title: string, userId) {
    switch (title) {
      case '分享':
        this.homepageService.getUserFeed(userId, 1, 2);
        this.homepageService.getUserAlbum(userId, 2, 1, 1, 10);
        this.homepageService.getUserAlbum(userId, 3, 1, 1, 10);
        break;
      case '粉丝':
        this.fansTab.getFollowsFun(userId);
        this.fansTab.getFansFun(userId);
        break;
      case '服务':
        this.homepageService.getServiceInfo(userId);
        break;
      case '赛事':
        this.homepageService.events(userId);
        break;
    }
  }

  /**
   * @description 关注
   * @author baoww765
   * @date 2017-12-16
   * @param userId 被关注人userid
   * */
  addCare(userId) {
    this.homepageService.addCareFans(userId).takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0') {
        this.isFollowed = 1;
      }
    });
  }

  /**
   * @description 获取图标
   * @param iconName 图标名
   * @author baoww765
   * @date 2017-12-16
   */
  getIcon(iconName: string) {
    return this.imgService.getIcon(iconName);
  }

  /**
   * @description 获取性别图标
   * @param sex 性别
   * @author baoww765
   * @date 2017-12-16
   */
  getImg(sex) {
    return sex === 1 ? this.imgService.getImg('boy.png') : (sex === 2 ? this.imgService.getImg('girl.png') : '');
  }

  /**
   * @description 取消关注
   * @author baoww765
   * @date 2017-12-16
   * @param userId 被关注人userid
   * */
  deleteCare(userId) {
    this.homepageService.DeleteCareFans(userId).takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0') {
        this.isFollowed = 0;
      }
    });
  }


  /**
   * @description 点击关注按钮
   * @author baoww765
   * @date 2017-12-16
   * @param userId 被关注人userid
   * */
  addDelete(userId) {
    if (this.isFollowed === 0) {
      this.addCare(userId);
    } else {
      this.deleteCare(userId);
    }
  }

  /**
   * @description 获取个人信息
   * @author baoww-765
   * @date 2017-12-16
   * @param userid
   * */
  getInfo(userid) {
    this.homepageService.getPeronInfo(userid).takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0') {
        this.personInfo = res.data;
        this.isFollowed = this.personInfo.isFollowed;
        this.isFollowedMe = this.personInfo.isFollowedMe;
        this.toolsService.hideLoading();
      }
    });
  }

  ngOnInit() {

  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
    this.toolsService.hideLoading();
  }

}
